import axios from "axios";
import { storage } from "wxt/storage";

async function addToken(url) {
    const pre = await storage.getItem("local:host");
    const _token = await storage.getItem("local:_token");
    let back = pre + url;

    if (back.indexOf("?") != -1) {
        back = back + "&_token=" + _token;
    } else {
        back = back + "?_token=" + _token;
    }

    return back;
}

async function post(url, param = {}) {
    url = await addToken(url);
    return axios.post(url, param);
}

async function get(url, param = {}) {
    url = await addToken(url);
    return axios({
        method: "get",
        url: url,
        params: param,
        responseType: "json",
    });
}

export { addToken, get, post };
